package game;

import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.util.*;
import javax.swing.Timer;


public class Screen extends JPanel implements ActionListener {
	public Screen() {
		addKeyListener(new TAdapter());
		
		board = new Board();
		blocks = new Vector<Block>();
		
		setFocusable(true);
		init();
	}
	
	public void init() {
		BlockFrame kwadratFrame = new BlockFrame(new int[][]{{1,1},{1,1}});
		Block kwadrat = new Block();
		kwadrat.addFrame(kwadratFrame);
		
		BlockFrame IFrame1 = new BlockFrame(new int[][]{{2},{2},{2},{2}});
		BlockFrame IFrame2 = new BlockFrame(new int[][]{{2,2,2,2}});
		Block I = new Block();
		I.addFrame(IFrame1);
		I.addFrame(IFrame2);
		
		BlockFrame KFrame1 = new BlockFrame(new int[][]{{3,3,3},{0,3,0}});
		BlockFrame KFrame2 = new BlockFrame(new int[][]{{3,0},{3,3},{3,0}});
		BlockFrame KFrame3 = new BlockFrame(new int[][]{{0,3,0},{3,3,3}});
		BlockFrame KFrame4 = new BlockFrame(new int[][]{{0,3},{3,3},{0,3}});
		
		Block K = new Block();
		K.addFrame(KFrame1);
		K.addFrame(KFrame2);
		K.addFrame(KFrame3);
		K.addFrame(KFrame4);
		
		BlockFrame ZFrame1 = new BlockFrame(new int[][]{{0,4,4},{4,4,0}});
		BlockFrame ZFrame2 = new BlockFrame(new int[][]{{4,0},{4,4},{0,4}});
		
		Block Z = new Block();
		Z.addFrame(ZFrame1);
		Z.addFrame(ZFrame2);
		
		BlockFrame SFrame1 = new BlockFrame(new int[][]{{5,5,0},{0,5,5}});
		BlockFrame SFrame2 = new BlockFrame(new int[][]{{0,5},{5,5},{5,0}});
		
		Block S = new Block();
		S.addFrame(SFrame1);
		S.addFrame(SFrame2);
		
		BlockFrame LFrame1 = new BlockFrame(new int[][]{{6,0},{6,0},{6,6}});
		BlockFrame LFrame2 = new BlockFrame(new int[][]{{6,6,6},{6,0,0}});
		BlockFrame LFrame3 = new BlockFrame(new int[][]{{6,6},{0,6},{0,6}});
		BlockFrame LFrame4 = new BlockFrame(new int[][]{{0,0,6},{6,6,6}});
		
		Block L = new Block();
		L.addFrame(LFrame1);
		L.addFrame(LFrame2);
		L.addFrame(LFrame3);
		L.addFrame(LFrame4);
		
		BlockFrame L2Frame1 = new BlockFrame(new int[][]{{0,7},{0,7},{7,7}});
		BlockFrame L2Frame2 = new BlockFrame(new int[][]{{7,0,0},{7,7,7}});
		BlockFrame L2Frame3 = new BlockFrame(new int[][]{{7,7},{7,0},{7,0}});
		BlockFrame L2Frame4 = new BlockFrame(new int[][]{{7,7,7},{0,0,7}});
		
		Block L2 = new Block();
		L2.addFrame(L2Frame1);
		L2.addFrame(L2Frame2);
		L2.addFrame(L2Frame3);
		L2.addFrame(L2Frame4);
		
		blocks.add(kwadrat);
		blocks.add(I);
		blocks.add(K);
		blocks.add(Z);
		blocks.add(S);
		blocks.add(L);
		blocks.add(L2);
		
		Random generator = new Random();
		block = blocks.get(generator.nextInt(blocks.size()));	
		nextBlock = blocks.get(generator.nextInt(blocks.size()));	
		block.setPos(4, 2);
		
		timer = new Timer(500, this);
        timer.start();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		
		Graphics2D g2 = (Graphics2D) g;
		
		g2.setColor(Color.WHITE);
		g2.fillRect(a, b, 200, 400);
		
		for( int i = 0; i < board.getBoard().length; i++ ) {
			for( int j = 0; j < board.getBoard()[0].length; j++) {
				if( board.getBoard()[i][j] != 0 ) {
					g2.setColor(getColor(board.getBoard()[i][j]));
					g2.fillRect(a+j*20+1, b+i*20+1, 18, 18);
				}
			}
		}
		
		for( int i = 0; i < block.getBlock().getHeight(); i++ ) {
			for( int j = 0; j < block.getBlock().getWidth(); j++ ) {
				if( block.getBlock().getFrame()[i][j] != 0 ) {
					g2.setColor(getColor(block.getBlock().getFrame()[i][j]));	                                              
					g2.fillRect(a+block.getX()*20+j*20+1, b+block.getY()*20+i*20+1, 18, 18);	
				}
			}
		}
		
		g.setColor(Color.black);
		g.drawString("Punkty: "+score, 210, 20);
		
		for( int i = 0; i < nextBlock.getFirstFrame().getHeight(); i++ ) {
			for( int j = 0; j < nextBlock.getFirstFrame().getWidth(); j++ ) {
				if( nextBlock.getFirstFrame().getFrame()[i][j] != 0 ) {
					int xx = 210 + (90 - nextBlock.getFirstFrame().getWidth() * 20) / 2;
					g2.setColor(getColor(nextBlock.getFirstFrame().getFrame()[i][j]));	                                              
					g2.fillRect(xx+j*20+1, 30+i*20+1, 18, 18);	
				}
			}
		}
	}
	
	public Color getColor(int i) {
		switch(i) {
		case 1: return Color.blue;
		case 2: return Color.darkGray;
		case 3: return Color.green;
		case 4: return Color.yellow;
		case 5: return Color.orange;
		case 6: return Color.magenta;
		case 7: return Color.red;
		}
		
		return Color.white;
	}

	public void actionPerformed(ActionEvent e) {
		if( !end ) {
			if( board.fits(block, block.getX(), block.getY()+1) ) {
				block.setPos(block.getX(), block.getY()+1);
			} else {
				board.addBlock(block);
				
				board.check();
				
				block = nextBlock;
				Random generator = new Random();
				nextBlock = blocks.get(generator.nextInt(blocks.size()));
				block.setPos(4, 0);
				
				if( !board.fits(block) ) {
					end = true;
				}
				
				score++;
			}
			
			repaint();
		}
	}
	
	private class TAdapter extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
	        int key = e.getKeyCode();
	        
	        if(key == KeyEvent.VK_UP) {
	           	block.nextFrame();
	           	if( block.getX() < 0 || block.getX() > 10-block.getBlock().getWidth() || !board.fits(block, block.getX(), block.getY()) )
	        		block.previousFrame();
	        } else if(key == KeyEvent.VK_DOWN) {
	        	board.fallBlock(block);
	        } else if(key == KeyEvent.VK_LEFT) {
	        	if( board.fits(block, block.getX()-1, block.getY()) )
	        		block.setPos(block.getX()-1, block.getY());
	        } else if(key == KeyEvent.VK_RIGHT) {
	        	if( board.fits(block, block.getX()+1, block.getY()) )
	        		block.setPos(block.getX()+1, block.getY());
	        }
	        
	        repaint();
		}
	}
	
	private Board board;
    private Block block;
    private Block nextBlock;
    private Vector<Block> blocks;
    
	private int a = 5, b = 5;
	private Timer timer;
	boolean end = false;
	int score = 0;

}
